package com.egao.common.publicly.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.mapper.CompanyMapper;
import com.egao.common.module.push.entity.PushMsg;
import com.egao.common.module.push.mapper.PushMsgMapper;
import com.egao.common.module.student.constants.StudentResumeConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.mapper.StudentMapper;
import com.egao.common.module.system.constants.EmailConstants;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.mapper.UserMapper;
import com.egao.common.module.system.service.EmailService;
import com.egao.common.publicly.mapper.PubliclyResumeMapper;
import com.egao.common.publicly.service.PubliclyResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 推送简历接口实现类
 *
 * @author pyx
 */
@Service
public class PubliclyResumeServiceImpl extends ServiceImpl<PubliclyResumeMapper, PushMsg> implements PubliclyResumeService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private PushMsgMapper pushMsgMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private EmailService emailService;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired
    private ElasticSearchService elasticSearchService;

    @Value("${minio.resumeDir}")
    private String resumeDir;

    @Value("${minio.minioDir}")
    private String minioDir;

    @Override
    public JsonResult pushResume(Integer id) throws MessagingException {
        if(id == null){
            return JsonResult.error("参数异常");
        }
        //获取推荐信息详情
        PushMsg pushMsg = pushMsgMapper.selectById(id);

        if(pushMsg == null){
            return JsonResult.error("没有获取到推送信息");
        }
        String xh = pushMsg.getXh();

        //通过学号找到学生简历
        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("xh", xh).last("limit 1"));
        //去掉http头
        String url = student.getResumeUrl();
        if (StudentResumeConstants.RESUME_TYPE.equals(student.getResumeType())) {
            url = resumeDir + "\\resume\\" + student.getId() + "\\resume.pdf";
        } else {
            if (StrUtil.isBlank(url)) {
                return JsonResult.error("未找到简历。请到学生生源填报中提交“标准版简历”或“自定义上传简历”");
            }

            String[] strings = url.split("9000");
            url = minioDir + strings[1];
            url = url.replaceAll("/", "\\\\");
        }

        File file = FileUtil.file(url);
        long fileLength = file.length();
        if (0L == fileLength) {
            return JsonResult.error("未找到简历。请到学生生源填报中提交“标准版简历”或“自定义上传简历”");
        }

        //查找到推送信息获取邮箱
        Company company = companyMapper.selectById(pushMsg.getCompanyId());
        if(company == null){
            return JsonResult.error("单位不存在");
        }

        User user = userMapper.selectById(company.getUserId());
        if (user == null) {
            return JsonResult.error("该公司暂无收件箱");
        }

        String email = user.getUsername();

        emailService.sendFileEmail(EmailConstants.TITLE_RESUME, company.getName(), student.getXm(), pushMsg.getTitle(), file, email);

        //更新推送信息
        pushMsg.setResumeFlag("0");
        pushMsgMapper.updateById(pushMsg);
        List<PushMsg> pushList = new ArrayList<>();
        pushList.add(pushMsg);
        elasticSearchService.savePushList(pushList, pushMsg.getBusinessId(), pushMsg.getType());

        return JsonResult.ok("发送成功");
    }
}
